/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IssuesList.java
 *
 * Created on May 25, 2010, 8:00:13 PM
 */

package org.dafe.partyband.gui.imports;

import java.awt.Color;
import java.awt.Component;
import java.util.List;
import javax.swing.AbstractListModel;
import org.dafe.partyband.gui.Utilities;
import org.dafe.partyband.importer.api.ISongImport.IImportIssue;
import org.dafe.partyband.importer.api.ISongImport.IImportIssue.Severity;
import org.jdesktop.swingx.color.ColorUtil;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import org.jdesktop.swingx.decorator.ComponentAdapter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.decorator.IconHighlighter;
import org.openide.util.NbBundle;

/**
 *
 * @author dafe
 */
public class IssuesList extends javax.swing.JPanel {
    private IssuesModel issuesModel = new IssuesModel();

    /** Creates new form IssuesList */
    public IssuesList() {
        initComponents();

        issuesList.setModel(issuesModel);

        HighlightPredicate defectPredicate = new IssuePredicate(Severity.DEFECT);
        HighlightPredicate warningPredicate = new IssuePredicate(Severity.WARNING);
        HighlightPredicate infoPredicate = new IssuePredicate(Severity.INFO);

        // defects coloring
        Color selBk = ColorUtil.interpolate(issuesList.getSelectionBackground(), Color.red, 0.25f);
        Color selFg = ColorUtil.computeForeground(selBk);
        issuesList.addHighlighter(new ColorHighlighter(defectPredicate, null,
                ColorUtil.interpolate(issuesList.getForeground(), Color.red, 0.25f),
                selBk, selFg));

        // warnings coloring
        selBk = ColorUtil.interpolate(issuesList.getSelectionBackground(), Color.yellow, 0.25f);
        selFg = ColorUtil.computeForeground(selBk);
        issuesList.addHighlighter(new ColorHighlighter(warningPredicate, null,
                ColorUtil.interpolate(issuesList.getForeground(), Color.yellow, 0.45f),
                selBk, selFg));

        // severity icons
        issuesList.addHighlighter(new IconHighlighter(defectPredicate,
                Utilities.getSeverityIcon(IImportIssue.Severity.DEFECT)));
        issuesList.addHighlighter(new IconHighlighter(warningPredicate,
                Utilities.getSeverityIcon(IImportIssue.Severity.WARNING)));
        issuesList.addHighlighter(new IconHighlighter(infoPredicate,
                Utilities.getSeverityIcon(IImportIssue.Severity.INFO)));
    }

    public void setContent (List<? extends IImportIssue> issues, String songName) {
        issuesModel.setIssues(issues);
        jXTitledSeparator1.setTitle(NbBundle.getMessage(IssuesList.class,
                "IssuesList.jXTitledSeparator1.title", songName)); // NOI18N

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXTitledSeparator1 = new org.jdesktop.swingx.JXTitledSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        issuesList = new org.jdesktop.swingx.JXList();

        jXTitledSeparator1.setToolTipText(org.openide.util.NbBundle.getMessage(IssuesList.class, "IssuesList.jXTitledSeparator1.toolTipText", new Object[] {})); // NOI18N
        jXTitledSeparator1.setTitle(org.openide.util.NbBundle.getMessage(IssuesList.class, "IssuesList.jXTitledSeparator1.title", new Object[] {"---"})); // NOI18N

        issuesList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        issuesList.setFocusable(false);
        jScrollPane1.setViewportView(issuesList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXTitledSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(210, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXTitledSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXList issuesList;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTitledSeparator jXTitledSeparator1;
    // End of variables declaration//GEN-END:variables


    private static class IssuesModel extends AbstractListModel {
        List<? extends IImportIssue> issues;

        public void setIssues (List<? extends IImportIssue> issues) {
            this.issues = issues;
            fireContentsChanged(this, 0, issues.size() - 1);
        }

        public int getSize() {
            return issues != null ? issues.size() : 0;
        }

        public Object getElementAt(int index) {
            return issues.get(index).getDescription();
        }

        public IImportIssue getIssueAt(int index) {
            return issues.get(index);
        }

    }

    private class IssuePredicate implements HighlightPredicate {
        private final Severity severity;

        public IssuePredicate(Severity severity) {
            this.severity = severity;
        }

        public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
            int modelRow = adapter.convertRowIndexToModel(adapter.row);
            return severity.equals(issuesModel.getIssueAt(modelRow).getSeverity());
        }
    }

}
